home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-2.iso / extra_2 / flmgmtcl.zip / XSTRING.CPP < prev    next >
C/C++ Source or Header  |  1995-11-02  |  7KB  |  273 lines

  1. // ==========================================================================
  2. //                         Class Implementation : CXString
  3. // ==========================================================================
  4.  
  5. // Source file : stringx.cpp
  6.  
  7. // Source : Periphere NV (R.Mortelmans)
  8. // Creation Date :        2nd November 1995
  9. // Last Modification : 2nd November 1995
  10.                           
  11. // //////////////////////////////////////////////////////////////////////////
  12.  
  13. #include "stdafx.h"
  14. #include "xstring.h"
  15. #include <ctype.h>
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char BASED_CODE THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. #define DECIMAL_CHARACTER_STRING_LENGTH   2  // Including 0-termination
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Definition of static members
  27. char pszDecimalDestination[2];
  28. char* pszDecimalDefault = TEXT(",");
  29. // ... The sequential-evaluation operator (comma operator) is used here
  30. char CXString::cDecimalCharacter(
  31.         (::GetProfileString(TEXT("Intl"), TEXT("sDecimal"), pszDecimalDefault, pszDecimalDestination, DECIMAL_CHARACTER_STRING_LENGTH),
  32.         *pszDecimalDestination));
  33.  
  34.  
  35. // Data members -------------------------------------------------------------
  36. // protected:
  37. // private:
  38.     
  39. // Member functions ---------------------------------------------------------
  40. // public:
  41.                            
  42. CXString::CXString()
  43.     {
  44.     }
  45.     
  46. CXString::CXString(const char* psz)
  47.     {
  48.     *((CString*)this) = psz;
  49.     }
  50.     
  51. CXString::CXString(const CString& sSource)
  52.     {
  53.     *((CString*)this) = sSource;
  54.     }
  55.     
  56. CXString::CXString(LONG nSource)
  57.     {
  58.     char pszBuffer[20];
  59.     *this = CXString(ltoa(nSource, pszBuffer, 10));
  60.     }
  61.  
  62. CXString::CXString(const CXString& sString)
  63.     : CString(sString)
  64.     {
  65.  
  66.     }
  67.  
  68. CXString& CXString::operator=(const CXString& sString)
  69.     {
  70.     CString::operator=(sString);
  71.  
  72.     return *this;
  73.     }
  74.          
  75. BOOL CXString::operator==(const CXString& s2) const
  76.     { 
  77.     // The =-operator of CString calls strcmp and thus only compares until the first NULL-character
  78.     // This function really compares the entire strings
  79.     return (GetLength() == s2.GetLength()) && 
  80.            (memcmp(m_pchData, s2.m_pchData, GetLength()) == 0);
  81.     }
  82.  
  83. void CXString::LTrim()
  84.     {    
  85.     int nStringIndex = 0;
  86.     int nLength = GetLength();
  87.     
  88.     while( (nStringIndex < nLength) && isspace(GetAt(nStringIndex)) )
  89.         nStringIndex++;
  90.     if (nStringIndex == nLength)
  91.         *this = CXString(TEXT(""));
  92.     else    
  93.         *this = Mid(nStringIndex);    
  94.     }
  95.  
  96. void CXString::RTrim()
  97.     {    
  98.     int nStringIndex = GetLength() - 1;
  99.     while((0 <= nStringIndex) && isspace(GetAt(nStringIndex)) )
  100.         nStringIndex--;
  101.     *this =  Left(nStringIndex + 1);    
  102.     }
  103.  
  104. void CXString::XTrim()
  105.     {                                                            
  106.     // Optimisation :
  107.     // The output string sOut is initialized by the value of the string
  108.     //     The real significant characters in sOut are from position 0 till nPosOut - 1
  109.     //     Characters are thus added by overwriting a character in sOut and incrementing nPosOut by 1.
  110.     //     Now memory is allocated only once (at initialisation of sOut)
  111.     // When you would start with an empty string and would concatenate characters
  112.     //     then extra memory must be allocated frequently (overhead).
  113.     
  114.     CXString sOut(*this);
  115.     int nPosIn = 0;
  116.     int nPosOut = 0;
  117.     BOOL bSpace = FALSE;
  118.     
  119.     while (nPosIn < GetLength())
  120.         {
  121.         if (isspace(GetAt(nPosIn)))
  122.             if (bSpace)
  123.                 {
  124.                 // ... Second white space character encountered
  125.                 //     Skip this character (do not copy)
  126.                 nPosIn++;
  127.                 }
  128.             else 
  129.                 {
  130.                 // ... First white space character encountered
  131.                 //     Copy space (all white space characters are replaced by a space)
  132.                 sOut.SetAt(nPosOut++, ' ');
  133.                 nPosIn++;
  134.                 bSpace = TRUE;
  135.                 }              
  136.         else
  137.             {    
  138.             // ... Copying a non white space character
  139.             sOut.SetAt(nPosOut++, GetAt(nPosIn++));
  140.             bSpace = FALSE;
  141.             }
  142.         }
  143.     *this = sOut.Left(nPosOut);
  144.     }
  145.     
  146. int CXString::GetInt() const 
  147.     {
  148.     return atoi(*this);
  149.     }
  150.  
  151. long CXString::GetLongInt() const 
  152.     {
  153.     return atol(*this);
  154.     }
  155.  
  156. BOOL CXString::IsInt()
  157.     {
  158.     for (int nIndex = 0; nIndex < GetLength(); nIndex++)
  159.         if (!isdigit(GetAt(nIndex)))
  160.             return FALSE;
  161.     return TRUE;        
  162.     }
  163.  
  164. BOOL CXString::IsNumber()
  165.     {
  166.     const char* pc = *this;
  167.     BOOL bDigit = FALSE;
  168.     
  169.     // Skip white space characters
  170.     while (isspace(*pc))
  171.         pc++;
  172.     // Skip sign
  173.     if ((*pc == '-') || (*pc == '+'))
  174.         pc++;
  175.     // Skip digits
  176.     while (isdigit(*pc))
  177.         {
  178.         pc++;
  179.         bDigit = TRUE;
  180.         }
  181.     // Skip decimal sign
  182.     if (*pc == cDecimalCharacter)
  183.         pc++;
  184.     if (!bDigit && !isdigit(*pc))
  185.         return FALSE;
  186.     // Skip digits
  187.     while (isdigit(*pc))
  188.         pc++;
  189.     // Skip exponent sign and rest
  190.     if ((*pc == 'E') || (*pc == 'e') || (*pc == 'D') || (*pc == 'd'))
  191.         {
  192.         pc++;
  193.         // Skip sign
  194.         if ((*pc == '-') || (*pc == '+'))
  195.             pc++;
  196.         // Skip digits
  197.         if (!isdigit(*pc))
  198.             // Exponent sign is not followed by digits
  199.             return FALSE;
  200.         while (isdigit(*pc))
  201.             pc++;
  202.         }
  203.     // Skip white space characters
  204.     while (isspace(*pc))
  205.         pc++;
  206.         
  207.     // Pointer should not be behind the end of the string
  208.     ASSERT(pc <= (((const char*)*this) + GetLength()));
  209.     
  210.     // Must be at the end of the string by now, otherwise the number is not valid
  211.     return (*pc == '\0');
  212.     }
  213.  
  214. void CXString::Format(const char* pszFormat, const char** rgpsz, int nString)
  215.     {
  216.     // NOTE: will not work for strings > 255 characters
  217.  
  218.     int nTotalLen = lstrlen(pszFormat);
  219.     for (int i = 0; i < nString; i++)
  220.         {
  221.         if (rgpsz[i] != NULL)
  222.             nTotalLen += strlen(rgpsz[i]);
  223.         }
  224.  
  225.     const char* pchSrc = pszFormat;
  226.     char* pchDest = GetBuffer(nTotalLen+1);
  227.     while (*pchSrc != '\0')
  228.         {
  229.         if (pchSrc[0] == '%' && (pchSrc[1] >= '1' && pchSrc[1] <= '9'))
  230.             {
  231.             i = pchSrc[1] - '1';
  232.             pchSrc += 2;
  233.             if (i >= nString)
  234.                 {
  235.                 TRACE1(TEXT("CXString::Format : Illegal string index requested %d\n"), i);
  236.                 *pchDest++ = '?';
  237.                 }       
  238.             else if (rgpsz[i] != NULL)
  239.                 {
  240.                 strcpy(pchDest, rgpsz[i]);
  241.                 pchDest += strlen(pchDest);
  242.                 }
  243.             }
  244.         else
  245.             {
  246.             *pchDest++ = *pchSrc++;
  247.             }
  248.         }
  249.     ReleaseBuffer((int)((const char*)pchDest - (const char*)this));
  250.             // Release will assert if we went too far
  251.     }
  252.  
  253. void CXString::BarToNull()
  254.     {
  255.     int nLength = GetLength();
  256.     LPTSTR pszData = GetBuffer(nLength);
  257.     while ((pszData = _tcschr(pszData, '|')) != NULL)
  258.         *pszData++ = '\0';
  259.     ReleaseBuffer(nLength);
  260.     ASSERT(GetLength() == nLength);
  261.     }
  262.  
  263. CXString::~CXString()
  264.     {
  265.     }
  266.     
  267. // protected:
  268.  
  269. // private:
  270.  
  271. // ==========================================================================
  272.     
  273.